package com.drawables;

import java.util.ArrayList;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.BitmapFactory;
import android.util.Log;

import com.aybived.AybivedConfig;
import com.aybived.main;
import com.interfaces.Collidable;

public class LevelTile extends Collidable {
	private ArrayList<Obstacle> obstacles;
	private double posX = 0;
	private boolean isVisible = false;
	private String levelStartTag;
	private String levelEndTag;

	public LevelTile(String respath, ArrayList<Obstacle> obstacles,String levelStartTag, String levelEndTag) {
		this.levelStartTag = levelStartTag;
		this.levelEndTag = levelEndTag;
		try {
			this.mask = BitmapFactory.decodeResource(main.resource, main.resource.getIdentifier("com.aybived:drawable/"+respath+"_mask", null, null));
			//addBitmap( BitmapFactory.decodeResource(main.resource, main.resource.getIdentifier("com.aybived:drawable/"+respath+"_view", null, null)) );
			//Bitmap tmp = BitmapFactory.decodeResource(main.resource, main.resource.getIdentifier("com.aybived:drawable/"+respath+"_view", null, null));
			this.obstacles = obstacles;
			addBitmap( "com.aybived:drawable/"+respath+"_view" );
			//initVertexData( getSpriteWidth(), Game.getScreenDimesions()[1]);
			
		} catch(Exception e) {
			Log.e("LevelTile.java","Load of bitmap: " + respath + " failed - " + e.getMessage());
		};
		
	}

	public String getLevelStartTag() {
		return levelStartTag;
	}
	
	public String getLevelEndTag() {
		return levelEndTag;
	}
	
	public void loadImages()
	{
		
	}
	
	public void unloadImages()
	{
		
	}
	public double getPosX() {
		return posX;
	}
	
	public int getMaskWidth() {
		return this.mask.getWidth();
	}
	
	public void setPosX(double d) {
		this.posX = d;
	}

	public void addObstacle(Obstacle obstacle) {
		obstacles.add(obstacle);
	}
	
//	public LinkedList<Obstacle> getObstacles() {
//		return obstacles;
//	}

	@Override
	public void draw(GL10 gl)
	{
		if(!isVisible)
			return;
		gl.glPushMatrix();
			gl.glTranslatef((float) posX, 0, 0);
			gl.glEnable(GL10.GL_BLEND);
			gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
			gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
			gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, UVBuffer);
			gl.glBindTexture( GL10.GL_TEXTURE_2D, texIDs.get(0) );
			gl.glFrontFace(GL10.GL_CCW);
			gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
			gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
			gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glDisable(GL10.GL_BLEND);
		gl.glPopMatrix();
		
	}

	public boolean isTileVisible()
	{
		return isVisible;
	}
	
	// add to screen
	public void setVisible(boolean flag)
	{
		isVisible = flag;
	}
	
	//reset and remove from screen
	public void resetPosition()
	{
		posX = main.getScreenDimesions()[0];
		isVisible = false;
	}
	
	public boolean isOutofBounds()
	{
		if(posX < -mask.getWidth())
			return true;
		return false;
	}
	
	@Override
	public void update(double dt) {
		if(!isVisible || isOutofBounds())
			return;
		posX -= (float)(AybivedConfig.GAME_SCROLL_SPEED * dt);
	}

}
